package com.zuozuo.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo4 {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
        test1();
        test2();
        test3();
    }

    public static void test1() {
        User user = new User();
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000000000; i++) {
            user.getName();
        }

        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);
    }

    public static void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Class.forName("com.zuozuo.reflection.User");
        User user = (User) c.newInstance();
        Method method = c.getMethod("getName", null);
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000000000; i++) {
            method.invoke(user, null);
        }

        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);

    }
    public static void test3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Class.forName("com.zuozuo.reflection.User");
        User user = (User) c.newInstance();
        Method method = c.getMethod("getName", null);
        method.setAccessible(true);
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000000000; i++) {
            method.invoke(user, null);
        }

        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);

    }
}
